<template>
  <div class="whole-bg">
    <div class="loginBlock">
      <div class="loginText">欢迎登录</div>
      <div class="formBlock">
        <el-form ref="form" :model="form"  :rules="rules">
          <el-form-item style="margin: 0" prop="username">
            <span class="formText">用户名</span>
            <el-input prefix-icon="el-icon-user-solid" v-model="form.username" style="display: flex;float: right;margin-top: 30px" size="normal"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <span class="formText">密米码</span>
            <el-input prefix-icon="el-icon-lock" v-model="form.password" show-password style="display: flex;float: right;margin-top: 30px" size="normal"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;margin-top: 10px" @click="login()" size="normal">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align: center;color: black"> <router-link to="/register" >还没有账号？点击注册</router-link></div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";  /*axios请求封装文件*/

export default {
  name: "Login",
  data() {
    return {
      form: {},
      /*表单验证*/
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ]
      }
    }
  },
  methods: {
    /*登录请求*/
    login() {
      request.post('/api/user/login', this.form).then((res) => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "登录成功"
          })
          let user = JSON.stringify(res.data)  /*js对象转json格式*/
          window.sessionStorage.setItem("user", user)
          this.$router.push('/main/user')  /*登录成功之后进行路由跳转*/
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.whole-bg {
  height: 100%;
  width: 100%;
  overflow: hidden; /*溢出隐藏，清除浮动*/
  background-image: url('../assets/images/infinity-2735110.jpg'); /*设置div容器为当前背景图片*/
  background-size: 100% 100%; /*设置背景图片占满整个屏幕*/
}

.loginBlock {
  width: 400px;
  margin: 100px auto;
}

.loginText {
  color: azure;
  font-size: 50px;
  font-weight: bold;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "宋体", Arial, sans-serif;
  text-align: center;
  padding: 20px 0;
}

.formText {
  color: azure;
  font-size: 20px;
  font-weight: bold;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  padding-top: 30px;
  position:absolute;
  bottom:0;
}

/*重写表单验证样式*/
>>> .el-form-item__error {
  float:right;
  font-size: 20px;
  font-weight: bold;
  right: 0;
  left: auto;
}

/*重写组件的样式*/
/*>>> .el-input__inner{
  height: 40px;
}*/
</style>

<style scoped>
/*触发单击事件时*/
.router-link-active {
  text-decoration: none;
}
/*默认样式*/
a {
  text-decoration: none;
  color: white;
  font-size: 20px;
  font-weight: bold;
}
</style>
